#include "mem_pool.h"
#include "def.h"

int mem_pool_test(void)
{
    printf("** mem_pool test:\n");

    uint32_t unit_size = 1024;
    uint32_t unit_total = 1024;
	mem_unit_t mu;
	memset(&mu, 0, sizeof(mu));
	mu.size = unit_size;
	mu.total = unit_total;
	mu.create = mem_unit_generic_create;
	mu.destroy = mem_unit_generic_destroy;
    mem_pool_t *mp = mem_pool_create(NULL, &mu);
    assert(mp);

    #define PTR_ARRAY_NUMS 2048
    void *ptr_array[PTR_ARRAY_NUMS] = {NULL};
    uint32_t i;

    for(i = 0; i < PTR_ARRAY_NUMS; ++i){
        ptr_array[i] = mem_unit_get(mp);
        assert(ptr_array[i]);
        printf("ptr(%u): 0x%x", i, (unsigned int)ptr_array[i]);
    }

    for(i = 0; i < PTR_ARRAY_NUMS; ++i){
        if(mem_unit_put(mp, ptr_array[i]) > 0){
            printf("mem_unit_release failed\n");
        }
    }

    mem_pool_destroy(&mp);

    #undef PTR_ARRAY_NUMS
    printf("** mem_pool test success\n");
    return 0;
}
